package com.gpframework.common.pay.leshua.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gpframework.common.pay.leshua.exception.BizRuntimeException;

public class LeshuaReconService {

	private Logger logger = LoggerFactory.getLogger(getClass());

	protected void buildChannelTransList(File file) {
		/*
		 * 每个代理商有两个对账文件 1）打款信息文件，文件格式：代理商id_日期.txt
		 * 2）交易明细文件，文件格式：代理商id_trade_日期.txt
		 */
		BufferedReader br = null;
		try {
			InputStreamReader read = new InputStreamReader(new FileInputStream(file), "utf-8");// 考虑到编码格式
			br = new BufferedReader(read);
			String str = null;
			while ((str = br.readLine()) != null) {
				boolean isTradeDetail = str.startsWith("BV");
				if (!isTradeDetail) {
					continue;
				}
//				String payTime = str.substring(9, 29).trim();
//				String settleDate = payTime.substring(0, 10).replace("-", "");
//				String channelOrderNo = str.substring(29, 46).trim();
//				String platOrderNo = null;
//
//				String channelMerchNo = str.substring(46, 57).trim();
//				String amount = str.substring(62, 72).trim();
//				String fee = str.substring(72, 83).trim();
//				String moneyDirection = str.substring(97, 104).trim();
//				String payWay = str.substring(110, 122).trim();
//				String payType = str.substring(127, 131).trim();
//				String certificateNo = str.substring(141, 168).trim();
//				String a = str.substring(175, 200).trim();
//				String b = str.substring(191, 195).trim();
//				logger.info("{},{},{},{},{},{},{},{},{},{},{},{}", payTime, settleDate, channelOrderNo, channelMerchNo,
//						amount, fee, moneyDirection, payWay, payType,certificateNo,a,b);
				
				String [] arr = str.split("\\s+");
				logger.info("length:{}",arr.length);
				String tmp = "";
				String split = ",";
				for(int i =1;i<arr.length;i++){
					tmp = tmp + arr[i] + split;
				}
				logger.info("{}",tmp);
			}
		} catch (IOException e) {
			throw new BizRuntimeException("读取乐刷对账文件内容失败", e);
		} finally {
			try {
				br.close();
			} catch (IOException e) {
				logger.error("error:", e);
			}
		}
	}

	public static void main(String[] args) {

		LeshuaReconService service = new LeshuaReconService();
		service.buildChannelTransList(new File("/Users/mark/Downloads/8857325_trade_2018-05-16.txt"));
	}

}
